package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.domain.CmDealerAccount;
import com.car.loan.project.cm.mapper.CmDealerAccountMapper;
import com.car.loan.project.cm.service.ICmDealerAccountService;
import com.car.loan.project.cm.util.select.DealerAccountBaseSelect;
import com.car.loan.project.cm.util.vo.SelectedAccountVo;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 经销商账户Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmDealerAccountServiceImpl implements ICmDealerAccountService {

    @Autowired
    private CmDealerAccountMapper cmDealerAccountMapper;

    /**
     * 查询经销商账户
     *
     * @param id 经销商账户ID
     * @return 经销商账户
     */
    @Override
    public CmDealerAccount selectCmDealerAccountById(Long id) {
        return cmDealerAccountMapper.selectById(id);
    }

    /**
     * 查询经销商账户列表
     *
     * @param cmDealerAccount 经销商账户
     * @return 经销商账户
     */
    @Override
    public List<CmDealerAccount> selectCmDealerAccountList(CmDealerAccount cmDealerAccount) {
        QueryWrapper<CmDealerAccount> queryWrapper = new QueryWrapper<>(cmDealerAccount);
        return cmDealerAccountMapper.selectList(queryWrapper);
    }

    @Override
    public List<SelectedAccountVo> selectedPage(DealerAccountBaseSelect select) {
        CmDealerAccount cmDealerAccount = new CmDealerAccount();
        cmDealerAccount.setDealerId(select.getDealerId());
        QueryWrapper<CmDealerAccount> queryWrapper = new QueryWrapper<>(cmDealerAccount);
        if (StringUtils.isNotEmpty(select.getName())) {
            queryWrapper.like("bank_name", select.getName());
        }
        queryWrapper.select("id,account_name,bank_name,bank_account_no,status");
        if (null == select.getOf() || SwitchStatus.OPEN.status == select.getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        List<CmDealerAccount> records = cmDealerAccountMapper
            .selectList(queryWrapper);
        List<SelectedAccountVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(data -> {
                SelectedAccountVo selectedAccountVo = new SelectedAccountVo();
                BeanUtils.copyBeanProp(selectedAccountVo, data);
                selectedAccountVo.setId(String.valueOf(data.getId()));
                selectedVos.add(selectedAccountVo);
            });
        }
        return selectedVos;
    }

    /**
     * 新增经销商账户
     *
     * @param cmDealerAccount 经销商账户
     * @return 结果
     */
    @Override
    public int insertCmDealerAccount(CmDealerAccount cmDealerAccount) {
        cmDealerAccount.setCreateTime(DateUtils.getNowDate());
        return cmDealerAccountMapper.insert(cmDealerAccount);
    }

    /**
     * 修改经销商账户
     *
     * @param cmDealerAccount 经销商账户
     * @return 结果
     */
    @Override
    public int updateCmDealerAccount(CmDealerAccount cmDealerAccount) {
        return cmDealerAccountMapper.updateById(cmDealerAccount);
    }

    /**
     * 删除经销商账户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmDealerAccountByIds(Collection<Long> ids) {
        return cmDealerAccountMapper.deleteBatchIds(ids);
    }

    /**
     * 删除经销商账户信息
     *
     * @param id 经销商账户ID
     * @return 结果
     */
    @Override
    public int deleteCmDealerAccountById(Long id) {
        return cmDealerAccountMapper.deleteById(id);
    }
}
